﻿using AutoMapper;
using Equipment.API.DTOs.RequestDto.OutgoinginspectionRequest;
using Equipment.API.DTOs.ResponseDto.OutgoinginspectionRequest;
using Equipment.Domain.Outgoinginspectionfolder;
using Equipment.Errorcode;
using Equipment.Errorcode.ErrorCode;
using Equipment.Infrastructure.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace Equipment.API.Application.Service.OutgoinginspectionService
{
    public class OutgoinginspectionServices
    {
        private readonly IOutgoinginspectionRepository _outgoinginspectionRepository;

        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="outgoinginspectionRepository"></param>
        /// <param name="mapper"></param>
        public OutgoinginspectionServices(IOutgoinginspectionRepository outgoinginspectionRepository, IMapper mapper)
        {
            _outgoinginspectionRepository = outgoinginspectionRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 新增出货检验信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult> AddOutgoinginspection(AddOutgoinginspectionResponseDto param)
        {
            var restult = _mapper.Map<Outgoinginspection>(param);

            var addoutgoing = await _outgoinginspectionRepository.InsertAsync(restult);

            return ApiResult.Ok(addoutgoing, "添加出货检验信息成功");
        }


        /// <summary>
        /// 质检管理信息查询
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult> GetOutgoinginspection(GetOutgoingRequestDto param, int pageNumber = 1, int pageSize = 2)
        {
            var result = (await _outgoinginspectionRepository.GetAllAsync(a => a.Isdelete == false)).AsQueryable();
            if(param.Inspectionnamenumber != null)
            {
                result = result.Where(a => a.Inspectionname.Contains(param.Inspectionnamenumber) || a.Inspectionnumber.Contains(param.Inspectionnamenumber));
            }

            if (!string.IsNullOrEmpty(param.Productnumber))
            {
                result = result.Where(a => a.Productnumber.Contains(param.Productnumber));
            }

            if(param.Inspectorsid != null)
            {
                result = result.Where(a => a.Inspectorsid == param.Inspectorsid);
            }
            // 应用分页
            var pagedResult = PaginationHelper.Paginate(result, pageNumber, pageSize);

            return ApiResult.Ok(pagedResult, "质检管理信息查询");
        }


        /// <summary>
        /// 修改出货检验信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult> UpdateOutgoinginspection(UpdateOutgoinginsRequestDto param)
        {
            var restult = await _outgoinginspectionRepository.GetAsync(a => a.Outgoinginspectionid == param.Outgoinginspectionid);
            if(restult == null){
                return ApiResult.Fail("未找到该出货检验信息");
            }
            var entity = _mapper.Map(param, restult);
            var update = await _outgoinginspectionRepository.UpdateAsync(entity);
            return ApiResult.Ok(update, "修改出货检验信息成功");
                            
        }
        /// <summary>
        /// 删除出货检验信息
        /// </summary>
        /// <param name="Outgoinginspectionid"></param>
        /// <returns></returns>
        public async Task<ApiResult> DeleteOutgoinginspection(int Outgoinginspectionid)
        {
            var restult = await _outgoinginspectionRepository.GetAsync(a => a.Outgoinginspectionid == Outgoinginspectionid);

            if (restult == null)
            {
                return ApiResult.Fail("未找到该出货检验信息");
            }
            restult.Isdelete = true;
            var update = await _outgoinginspectionRepository.UpdateAsync(restult);
            return ApiResult.Ok(update, "删除出货检验信息成功");
        }




    }
}
